package com.caow.androidktbase.ui.second

import android.graphics.Bitmap
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.caow.androidktbase.BR
import com.caow.androidktbase.R
import com.caow.androidktbase.databinding.ActivityQrBinding
import com.caow.androidktbase.router.ROUTER_QR
import com.caow.baselibrary.BaseActivity
import com.google.zxing.BarcodeFormat
import com.google.zxing.WriterException
import com.journeyapps.barcodescanner.BarcodeEncoder


/**
 * @Author caow
 * 2025/1/9
 */
@Route(path = ROUTER_QR)
class QRActivity : BaseActivity<ActivityQrBinding, QRViewModel>() {
    override fun initLayoutId(): Int = R.layout.activity_qr
    override fun initViewModelId(): Int = BR.vm

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.iv.setImageBitmap(generateQRCode("hhhhhhhhh"))
    }

    private fun generateQRCode(text: String) : Bitmap? {
        val barcodeEncoder = BarcodeEncoder()
        try {
            val bitMatrix = barcodeEncoder.encode(text, BarcodeFormat.QR_CODE, 200, 200)
            val bitmap = barcodeEncoder.createBitmap(bitMatrix)
            return bitmap
        } catch (e: WriterException) {
            e.printStackTrace()
        }
        return null
    }
}